package com.ez.supervise.action.biz;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.model.biz.DemocracyScore;
import com.ez.supervise.model.biz.GoalDetail;
import com.ez.supervise.model.biz.OrgDemocracy;
import com.ez.supervise.model.dto.LoginUser;
import com.ez.supervise.model.dto.QueryDemocracyDto;
import com.ez.supervise.model.sys.Org;
import com.ez.supervise.service.GoalDetailService;
import com.ez.supervise.service.OrgService;
import com.ez.supervise.util.Format;
import com.ez.supervise.util.FreemarkerUtil;

public class StatisticCheckAction extends BaseJsonAction {

    private static final long serialVersionUID = -5237037468195860016L;
    private GoalDetailService goalDetailService;
//    private DemocracyGoalService democracyGoalService;
//    private DemocracyScoreService democracyScoreService;

    private OrgDemocracy orgDemocracy;
    private OrgService orgService;

    private Page page;
    private Integer year;
    private String orgType;
    private Long categoryId;
    
    private List<Org> orgList;
    private List<GoalDetail> goalDetailList;
    private static List<DemocracyScore> democracyScoreList;
    private Map<String, String> democracyMap;

    public String statisticCheck() throws Exception {
	orgList = orgService.findOrgByType(orgType);
	if (year == null) {
	    year = Calendar.getInstance().get(Calendar.YEAR);
	}
	goalDetailList = goalDetailService.findGoalByCategroy(categoryId);
//	List democracyList = orgDemocracyService.queryDemocracy(year, orgType, queryType, checkManId);
//	democracyMap = convertMap((List<QueryDemocracyDto>) democracyList, queryType);
	
	
	return SUCCESS;
    }

   
    private boolean createHtml(Map result) {
	boolean flag = FreemarkerUtil.createHtml("index-democracy.ftl", result, WEB_SITE_PATH, "index-democracy.htm");
	return flag;
    }

    /**
     * @param democracyList
     * @return
     */
    private Map<String, String> convertMap(List<QueryDemocracyDto> democracyList, String queryType) {
	if (democracyList == null) {
	    return null;
	} else {
	    Map<String, String> resultMap = new HashMap<String, String>();
	    for (QueryDemocracyDto dto : democracyList) {
		if (dto != null) {
		    resultMap.put(dto.getOrgId() + "_" + dto.getScoreId(), Format.format(dto.getScore(), queryType));
		}
	    }
	    return resultMap;
	}
    }

    public void setYear(Integer year) {
	this.year = year;
    }


    public void setOrgType(String orgType) {
	this.orgType = orgType;
    }

    public String getOrgType() {
	return orgType;
    }

   
    public void setOrgService(OrgService orgService) {
	this.orgService = orgService;
    }

   

    public OrgDemocracy getOrgDemocracy() {
	return orgDemocracy;
    }

    public void setOrgDemocracy(OrgDemocracy orgDemocracy) {
	this.orgDemocracy = orgDemocracy;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

    public void setOrgList(List<Org> orgList) {
	this.orgList = orgList;
    }

    public List<Org> getOrgList() {
	return orgList;
    }
//
//    public List<DemocracyScore> getDemocracyScoreList() {
//	if (democracyScoreList == null) {
//	    democracyScoreList = democracyScoreService.findAll();
//	}
//	return democracyScoreList;
//    }

    public static void clearDemocracyScoreList() {
	if (democracyScoreList != null) {
	    democracyScoreList.clear();
	    democracyScoreList = null;
	}
    }



//    public String calcSorce(Long orgId, Long scoreId) {
//	if (democracyMap == null) {
//	    if ("point".equals(queryType)) {
//		return "0.00";
//	    } else {
//		return "0";
//	    }
//	} else {
//	    String reult = democracyMap.get(orgId + "_" + scoreId);
//	    if (reult == null) {
//		if ("point".equals(queryType)) {
//		    return "0.00";
//		} else {
//		    return "0";
//		}
//	    } else {
//		return reult;
//	    }
//	}
//    }

    public static void main(String[] args) {

    }

    public final static List<OrgDemocracy> makeDemocracy(String data, Integer year, LoginUser loginUser) {
	String temp = "";
	String[] tempArray;
	List<OrgDemocracy> dataList = new ArrayList<OrgDemocracy>();
	OrgDemocracy orgDemocracy = null;
	String[] arrayData = data.split(",");
	if (ArrayUtils.isEmpty(arrayData)) {
	    return dataList;
	}
	for (int i = 0; i < arrayData.length; i++) {
	    temp = arrayData[i];
	    tempArray = temp.split("=");
	    temp = tempArray[0];
	    orgDemocracy = new OrgDemocracy();
	    orgDemocracy.setScoreId(Long.valueOf(tempArray[1]));
	    tempArray = temp.split("_");
	    orgDemocracy.setOrgId(Long.valueOf(tempArray[2]));
	    orgDemocracy.setGoalId(Long.valueOf(tempArray[3]));
	    orgDemocracy.setYear(year);
	    orgDemocracy.setCheckManId(loginUser.getDemocracyId());
	    orgDemocracy.setUserId(loginUser.getUserId());
	    dataList.add(orgDemocracy);
	}
	return dataList;
    }


    public void setCategoryId(Long categoryId) {
	this.categoryId = categoryId;
    }
    public Long getCategoryId() {
	return categoryId;
    }

     
 

}
